<?php
$url = new Url();
$arrTypeSanPham = LoadConfig::$arrayTypeSanPham; 
$link_url = "http://images.az24.vn:8080/upload.php";  
?>
<link href="<?=Yii::app()->params['urlRs']; ?>/js/swfupload/css/default.css" rel="stylesheet" type="text/css" />  
<script type="text/javascript" src="<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/js/handlers.js"></script>    
<div class="left">
    <?php $this->widget("AdminMenuDoithuong")?>
</div>
<div class="right-main">
    <div class="righ-cont">
        <div class="right">
            <div class="title_style"> <span><b>Thêm sản phẩm</b></span></div>
            <div class="tool clearfix bottom15">
                <ul class="form">
                    <li class="clearfix">
                        <label><strong>Loại sản phẩm :</strong></label>
                        <div class="filltext">
                            <select style="width:235px; height: 24px;" id="type" name="type">
                                <?php foreach($arrTypeSanPham as $key=>$value){?>
                                    <option value ="<?=$key?>"><?=$value?></option>
                                    <?php }?> 
                            </select>
                        </div>
                    </li>
                    <li class="clearfix">
                        <label><strong>Tên sản phẩm :</strong> </label>
                        <div class="filltext">
                            <input type="text" style="width:223px" id="name" value="">
                        </div>
                    </li>
                    <li class="clearfix">
                        <label><strong>Giá :</strong> </label>
                        <div class="filltext">
                            <input type="text" style="width:150px" id="price"> <strong>dong</strong>
                        </div>
                    </li>
                    <li class="clearfix">
                        <label><strong>Hình ảnh :</strong> </label>
                        <div class="filltext">
                            <div>
                                <input type="text" id="txtFileName" disabled="disabled" style="background-color: #FFFFFF; width: 223px;" value=""/>                            
                                <span id="spanButtonPlaceholder"></span>
                            </div>                        
                            <div class="flash" id="divFileProgressContainer"></div>
                            <div id="thumbnails"></div>
                        </div> 
                    </li>
                    <li class="clearfix">
                        <label>&nbsp; </label>
                        <div class="filltext">
                            <input type="button" value="Thêm mới" class="button" onclick="insertRow()">
                        </div>
                    </li>
                </ul>
            </div>
            <div id="result_submit"></div>
        </div>
    </div>
</div>
<script type="">
    var swfu;
    window.onload = function () {
        swfu = new SWFUpload({
            // Backend Settings
            upload_url: "<?=$link_url ?>",
            post_params: "resume_file",

            // File Upload Settings
            file_size_limit : "4 MB",
            file_types : "*.jpg;*.jpeg;*.bmp;*.png;*.gif;*.tiff",
            file_types_description : "JPG Images",
            file_upload_limit : "0",

            file_queue_error_handler : fileQueueError,
            file_dialog_complete_handler : fileDialogComplete,
            upload_progress_handler : uploadProgress,
            upload_error_handler : uploadError,
            upload_success_handler : uploadSuccess,
            upload_complete_handler : uploadComplete,

            // Button Settings
            button_image_url : "<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/XPButtonUploadText_61x22.png",
            button_placeholder_id : "spanButtonPlaceholder",
            button_width: 61,
            button_height: 22,

            // Flash Settings
            flash_url : "http://images.az24.vn:8080/swfupload.swf",
            //flash_url : "<?php echo Yii::app()->params['urlRs']; ?>/swfupload/swfupload.swf",

            custom_settings : {
                upload_target : "divFileProgressContainer"
            },

            // Debug Settings
            //debug: true
        });
    };
    
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
    var count = 0;
    var filename = "";
    var filepath = "";
    function getdata(data_json){                   
        var json = $.parseJSON(data_json);
        var code = json.code; 
              
        if(code == '105'){ 
            count = 1;          
            filename = json.data["filename"];
            filepath = json.data["path"]  + "/" + ""+filename;
            path = "http://images.az24.vn"+ json.data["path"]  + "/" + ""+filename;                       
            $("#txtFileName").val(filename);                      
        }else{
            var msg = json.msg;                
            alert(msg);    
        }
    }
    
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
    function insertRow(){
        
        var name = $("#name").val();  
        var type = $("#type").val();    
        var price = $("#price").val();    
        if(name == ""){
            $('div#result_submit').html('<font style="color:red">Tên sản phẩm được bỏ trống!</font>'); 
            $("#name").focus();
        }else if(price == ""){
            $('div#result_submit').html('<font style="color:red">Giá sản phẩm không được bỏ trống!</font>'); 
            $("#price").focus();
        }else if( parseInt(price) < 0){
            $('div#result_submit').html('<font style="color:red">Giá sản phẩm phải là số dương!</font>'); 
            $("#price").focus();
        }else{
            var strUrl = "<?=$url->createUrl("doithuong/insertRow")?>";
            $.ajax({
                type: "POST",
                url: strUrl,
                data: { name        :name,
                        type        :type,
                        price       :price,
                        image_url   :filepath
                },
                success: function(msg){  
                    if(msg!=0){     
                        $('div#result_submit').html('<font style="color:blue">Thêm sản phẩm thành công</font>'); 
                        location.reload(); 
                    }else{
                        $('div#result_submit').html('<font style="color:red">Gặp lỗi trong quá trình xử lý. Vui lòng thử lại sau!</font>'); 
                        location.reload();
                    } 
                }
            });            
        }      
                        
    }
</script>


